package huawei;

import java.util.Stack;

/**
 * 解压报文
 */
public class Q113 {

    public static void main(String[] args) {
//        String s = "5[aa2[mn]]";
        String s = "aaa3[k]mm2[mn2[uz]]aaa";
        Stack<Character> stack = new Stack<>();

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c != ']') {
                stack.push(c);
            } else {
                StringBuilder sb = new StringBuilder();
                while (stack.peek() != '[') {
                    sb.insert(0,stack.pop());
                }
                stack.pop();
                StringBuilder num = new StringBuilder();
                while (!stack.isEmpty() && stack.peek() >= '0' && stack.peek() <= '9') {
                    num.append(stack.pop());
                }
                int repeat = Integer.parseInt(num.reverse().toString()); // 重复次数

                StringBuilder subStr = new StringBuilder();
                for (int j = 0; j < repeat; j++) {
                    subStr.append(sb);
                }

                for (int j = 0; j < subStr.length(); j++) {
                    stack.push(subStr.charAt(j));
                }
            }
        }
        StringBuilder sb = new StringBuilder();
        while (!stack.isEmpty()) {
            sb.insert(0, stack.pop());
        }
        System.out.println(sb);
    }
}
